package com.yk._12ForkJoin;

import java.util.concurrent.RecursiveTask;

public class ForkJoin01 extends RecursiveTask<Long> {


    private long start;
    private long end;
    private Long tmp = 100000L;


    public ForkJoin01(long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        long total = 0;
        if (end - start < tmp) {
            for (long i = start; i <= end; i++) {
                total += i;
            }
//            System.out.println(Thread.currentThread().getName()); fork

            return total;
        } else {
            long middle = (start + end) / 2;

            ForkJoin01 forckJoin01 = new ForkJoin01(start, middle);
            forckJoin01.fork();
            ForkJoin01 forckJoin02 = new ForkJoin01(middle + 1, end);
            forckJoin02.fork();
            return forckJoin01.join() + forckJoin02.join();
        }

    }

}
